//
//  ViewController.swift
//  homework1
//
//  Copyright © 2017年 14110600515wupengxu All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var index: Int! = 0
    
    lazy var allArr:NSMutableArray = { () -> NSMutableArray in
        
        let tempallArr = NSMutableArray()
        
        let strPath = Bundle.main.path(forResource: "Singer", ofType: "plist")
        
        let arr: NSMutableArray = NSMutableArray(contentsOfFile: strPath!)!
        
        for dict in arr{
            let model: SingerModel = SingerModel.SingerModelDict(dict as! NSDictionary)
            tempallArr.add(model)
        }
        return tempallArr
    }()
    
    lazy var titleLabel: UILabel = { ()->UILabel in
        let temptitleLabel = UILabel(frame: CGRect(x: 60, y: 60, width: 200, height: 50))
        temptitleLabel.backgroundColor = UIColor.red
        self.view.addSubview(temptitleLabel)
        return temptitleLabel
    }()
    
    lazy var imgview: UIImageView = { ()-> UIImageView in
        let tempimgview = UIImageView(frame: CGRect(x: 60, y: 120, width: 200, height: 200))
        tempimgview.backgroundColor = UIColor.red
        self.view.addSubview(tempimgview)
        return tempimgview
    }()
    
    lazy var btnBack: UIButton = { ()-> UIButton in
        let tempbtnBack = UIButton(frame: CGRect(x: 60, y: 330, width: 95, height: 50))
        tempbtnBack.setTitle("上一张", for: UIControlState.normal)
        tempbtnBack.addTarget(self, action: #selector(setBack), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnBack)
        return tempbtnBack
    }()
    
    lazy var btnNext: UIButton = { ()-> UIButton in
        let tempbtnNext = UIButton(frame: CGRect(x: 165, y: 330, width: 95, height: 50))
        tempbtnNext.setTitle("下一张", for: UIControlState.normal)
        tempbtnNext.addTarget(self, action: #selector(setNext), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnNext)
        return tempbtnNext
    }()
    
    @objc func setBack(){
        if self.index > 0 && self.index <= self.allArr.count - 1 {
            self.index = self.index - 1
            let model: SingerModel = self.allArr[self.index] as! SingerModel
            let titleStr = NSString(string: "\(model.name!)\(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgview.image = UIImage(named: model.pic as String)
            self.btnNext.isEnabled = true
            self.btnNext.backgroundColor = UIColor.blue
            if self.index == 0{
                self.btnBack.isEnabled = false
                self.btnBack.backgroundColor = UIColor.gray
            }
        }
    }
    
    @objc func setNext(){
        if self.index >= 0 && self.index <= self.allArr.count - 1 {
            self.index = self.index + 1
            let model: SingerModel = self.allArr[self.index] as! SingerModel
            let titleStr = NSString(string: "\(model.name!)\(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgview.image = UIImage(named: model.pic as String)
            self.btnBack.isEnabled = true
            self.btnBack.backgroundColor = UIColor.blue
            if self.index == self.allArr.count - 1{
                self.btnNext.isEnabled = false
                self.btnNext.backgroundColor = UIColor.gray
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showCurrentPage(iPage: self.index)
    }
    
    func showCurrentPage(iPage: Int){
        let model: SingerModel = self.allArr[iPage] as! SingerModel
        let titleStr = NSString(string: "\(model.name!)\(self.index+1)/\(self.allArr.count)")
        self.titleLabel.text = titleStr as String
        self.titleLabel.textAlignment = .center
        self.imgview.image = UIImage(named: model.pic as String)
        self.btnBack.backgroundColor = UIColor.blue
        self.btnNext.backgroundColor = UIColor.blue
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
